﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using System.Data;
using Telerik.Web.UI;
using System.IO;

namespace HCFramework.Page.AdminTool.TrangManagement
{
    public partial class TrangManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // An cac control chua dung den
                PanelAddAndEdit.Visible = false;
                PanelGrid.Visible = true;
            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Trangs
                         orderby a.URL
                         select a;
            DataTable dt = M.Common.LINQToDataTable(result);
            dt.Columns.Add("RoleName");
            foreach (DataRow row in dt.Rows)
            {
                int trangID = int.Parse(row["TrangID"].ToString());
                row["RoleName"] = C.TrangController.GetRoleName(trangID);
                row.AcceptChanges();
            }
            RadGridMain.DataSource = dt;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = true;
                ButtonUpdate.Visible = false;
            }
            if (e.CommandName == "Xoa")
            {
                GridDataItem item = (GridDataItem)e.Item;
                C.TrangController.Delete(int.Parse(item["TrangID"].Text));
            }
            if (e.CommandName == "Sua")
            {
                GridDataItem item = (GridDataItem)e.Item;
                LabelID.Text = item["TrangID"].Text;
                BindEditForm();

                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = false;
                ButtonUpdate.Visible = true;
            }
        }
        private void BindEditForm()
        {
            int id = int.Parse(LabelID.Text);
            Trang tr = C.TrangController.GetByID(id);
            TextBoxURL.Text = tr.URL;
            TextBoxTitle.Text = tr.Title;
            TextBoxMeta.Text = tr.Meta;
            TextBoxDescription.Text = tr.Description;
        }
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            // Insert a record
            Trang tr = new Trang();
            tr.URL = TextBoxURL.Text;
            tr.Title = TextBoxTitle.Text;
            tr.Meta = TextBoxMeta.Text;
            tr.Description = TextBoxDescription.Text;
            C.TrangController.Insert(tr);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            // Update
            int id = int.Parse(LabelID.Text);
            HCDataContext db = new HCDataContext();
            Trang tr = C.TrangController.GetByID(id, db);
            tr.URL = TextBoxURL.Text;
            tr.Title = TextBoxTitle.Text;
            tr.Meta = TextBoxMeta.Text;
            tr.Description = TextBoxDescription.Text;
            C.TrangController.Update(tr, db);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }

        protected void LinkButtonAutoFind_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = Directory.CreateDirectory(Server.MapPath("~/"));
            AutoFindRecurse(di);
        }

        public void AutoFindRecurse(DirectoryInfo di)
        {
            // files
            foreach (FileInfo fi in di.GetFiles("*.aspx"))
            {
                Trang tr = new Trang();
                tr.URL = fi.FullName.Replace(Server.MapPath("~/"), "~/").Replace("\\","/");
                C.TrangController.Insert(tr);
            }

            // subdirectories
            foreach (DirectoryInfo subDi in di.GetDirectories())
            {
                AutoFindRecurse(subDi);
            }
        }

        protected void LinkButtonAddRole_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int trangId = int.Parse(dataItem["TrangID"].Text);
                int roleId = int.Parse(ChooseRole1.DropDownList1.SelectedValue);

                TrangRole trangRole = new TrangRole();
                trangRole.RoleID = roleId;
                trangRole.TrangID = trangId;
                C.TrangRoleController.Insert(trangRole);
            }
        }

        protected void LinkButtonDeleteRole_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int trangId = int.Parse(dataItem["TrangID"].Text);
                int roleId = int.Parse(ChooseRole1.DropDownList1.SelectedValue);

                TrangRole trangRole = C.TrangRoleController.Get(trangId, roleId);
                if (trangRole != null) C.TrangRoleController.Delete(trangRole.TrangRoleID);
            }
        }
    }
}